var th;//当前地址
var defaultTh;//默认ID
var OrderId;//当前订单ID
var Money;//账户余额
var Total;//订单商品总价
var userId=1;//用户Id

//页面加载完毕
window.onload=function(){
    th=$($('.ui-icon-checked-s')).parent();
    defaultTh=th;
    setDefaultAds();
    getInfo();
}


//获取并显示相关信息
function getInfo(){
    OrderId=window.location.search.substr(9);
    $.post(handleUrl+"/Payment/getInfo", {isUserId:userId,isOrderId:OrderId},
        function(response){
            Money=response['Money'];
            Total=response['OrderTotal'];
            pMoney.innerHTML="余额："+Money;
            payMoney.innerHTML="支付："+parseInt(Total).toFixed(2);
        },'json'
    );
}


//选择其它地址
function selectAddress(){
    if($('#div_AddressSet').find('li').length==0){
        tipMsg('当前没有可选地址，请先创建!','warn');
        $('#input_new').prop('checked',true);
        $('#input_new').click();
        return;
    }
    actionDialog(2);
    
}

function setTh($this){
    th=$this;
    defaultValue();
    $(th).find('i').attr('class','ui-icon-checked-s');
    $('.ui-dialog').addClass('show').hide();
    setCurrent();
}

//清理默认值
function defaultValue(){
   $('#div_AddressSet').find('i').attr('class','');
}

//设置当前地址
function setCurrent(){
    if(th.length<=0){
        tipMsg("请选择地址!",'info');
        return;
    }
    $temp=$('#currentAds').find('p');
    $($temp[0]).text($($(th).find('p')[0]).text())
    $($temp[1]).text($($(th).find('p')[1]).text())
    $($('#currentAds').find('i')).attr('class','ui-icon-checked-s');
    
}

//设置默认地址
function setDefaultAds(){
    if(defaultTh.length==0){
        tipMsg("您没有设置默认地址,请选择其它地址或创建!",'info');
        $('#input_other').prop('checked',true);
        $('#input_other').click();
        return;
    }
    th=defaultTh;
    defaultValue();
    defaultTh.find('i').attr('class','ui-icon-checked-s');
    setCurrent();
}

//新地址
function newAds(){
    /*
    tipMsg('请前往个人中心设置新地址!!');
    $('#input_default').prop('checked',true);
    */
    actionDialog(3);
    
} 

//顶部提示
function tipMsg($content,$type){
    var el;
    el=$.tips({
        content:$content,
        stayTime:4000,
        type:$type
    })
    
}

//其它支付方式
function payPlan(){
    tipMsg("此支付方式尚未开通!","warn");
    $('#input_Payqb').prop('checked',true);
}

//支付：
function isPayMoney(){
     if(th.length<=0){
        tipMsg("请选择地址!",'info');
        return;
    }
    actionDialog(1);
}

//验证密码
function checkPasswd(){
    $pwd=$('#input_passwd').val();
    if($pwd.length<6){
        tipMsg("密码长度不正确!","warn");
        return;
        
    }
    //alert($(th).attr('id'));
    $.post(handleUrl+"/Payment/checkPasswd", {isUserId:userId,isPasswd:$pwd,isOrderId:OrderId,AddressId:$(th).attr('id')},
            function(response){
                
                switch (response) {
                    case 1:
                        tipMsg('支付成功!','success');
                        popLoading();
                        break;
                    case 2:
                        tipMsg('密码错误!','warn');
                        break;
                    case 3:
                        tipMsg('余额不足!','info');
                        break;
                    case 4:
                        tipMsg('订单不存在!','warn');
                        break;
                    case 5:
                        tipMsg('支付失败,库存不足!','warn');
                        break;
                    default:
                        break;
                }
                $('.ui-dialog').addClass('show').hide();
            },'json'
    );
}

//弹出加载框
function popLoading(){
    var hideobj=document.getElementById("hidebg");
    hidebg.style.display="block";  //显示隐藏层
    hidebg.style.height=(window.screen.height)+"px";  //设置隐藏层的高度为当前页面高度
    
    myel=$.loading({
        content:'前往订单中心...',
    })
    setTimeout(function(){
        myel.loading("hide");
    },2000);
    
    myel.on("loading:hide",function(){
        document.getElementById("hidebg").style.display="none";
        location.href=handleUrl+"/Order/loadOrder";
    });

}

//dialog操作 (1)密码 (2)选择地址 (3)新地址
function actionDialog(state){
   
    $('.ui-dialog').addClass('show').show();
    $('#div_AddressSet').hide();
    $('#div_NewAddress').hide();
    switch (state) {
        case 1:
            $('#div_Password').show();
            $('#div_AddressSet').hide();
            $('#div_NewAddress').hide();
            break;
        case 2:
            $('#div_Password').hide();
            $('#div_AddressSet').show();
            $('#div_NewAddress').hide();            
            break;
        case 3:
            $('#div_Password').hide();
            $('#div_AddressSet').hide();
            $('#div_NewAddress').show();             
            break;
        default:
            break;
    }
}

//添加地址
function actionAds(){
    var uId=1;
    
    var aName=$('#adsName').val();
    var aPhone=$('#adsPhone').val();
    var aAddress=$('#adsAddress').val();
    var myreg = /^1[3458]\d{9}$/;
    if(aName=="" || aPhone=="" || aAddress==""){
        $('.ui-dialog').addClass('show').hide();
        tipMsg("内容不能为空!","warn");
        return; 
    }else if(!myreg.exec(aPhone)){
        tipMsg("手机号码不规范!","warn");
        $('#adsPhone').val('');
        $('#adsPhone').focus();
        return; 
    }
    $.post(handleUrl+"/UserInfo/actionAddress", 
        {InfoId:'null',
        isUserId:uId,
        TakeName:aName,
        TakePhone:aPhone,
        TakeAddress:aAddress,
        YnDefault:'ui-icon-checked-s',
        isState:'1',},
        function(response){
            if(response>0){
                //alert(response);
                location.href=(handleUrl+"/Payment/loadAddress?OrderId="+OrderId);
            }
        },'json'
    );
    
}


    
